package com.simple.gateway.core.datasource.connection;

import cn.hutool.db.DbUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.handler.EntityListHandler;
import cn.hutool.db.sql.SqlExecutor;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.fastjson.JSON;
import com.simple.gateway.core.datasource.Connection;
import com.simple.gateway.core.mapping.HttpStatement;
import com.simple.gateway.core.session.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 项目: simple-gateway-core
 *
 * 功能描述: db连接抽象类
 *
 * @author: WuChengXing
 *
 * @create: 2023-08-15 07:59
 **/
public abstract class DbConnection implements Connection {

    private final Logger logger = LoggerFactory.getLogger(DbConnection.class);

    private final Configuration configuration;
    private final HttpStatement httpStatement;

    public DbConnection(Configuration configuration, HttpStatement httpStatement) {
        this.configuration = configuration;
        this.httpStatement = httpStatement;
    }

    protected java.sql.Connection getConnection() {
        DruidDataSource dataSource = configuration.getDataSource(httpStatement.getApplication());
        DruidPooledConnection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException sqlException) {
            logger.error("获取数据源失败：【{}】", JSON.toJSONString(httpStatement), sqlException);
            sqlException.printStackTrace();
        }
        return connection;
    }

    protected void closeConn(java.sql.Connection connection) {
        try {
            connection.close();
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
    }

    java.sql.Connection conn = null;

    protected List<Entity> exec(String sql, Map<String, String> args) {
        try {
            conn = this.getConnection();
            /* 执行查询语句，返回实体列表，一个Entity对象表示一行的数据，Entity对象是一个继承自HashMap的对象，存储的key为字段名，value为字段值 */
            List<Entity> entityList = SqlExecutor.query(conn, sql, new EntityListHandler(), args);
            logger.info("{}", entityList);
            return entityList;
        } catch (SQLException e) {
            logger.error("SQL error!", e);
        } finally {
            DbUtil.close(conn);
        }
        return Collections.emptyList();
    }

    protected List<Entity> exec(String sql, Object[] args) {
        try {
            conn = this.getConnection();
            /* 执行查询语句，返回实体列表，一个Entity对象表示一行的数据，Entity对象是一个继承自HashMap的对象，存储的key为字段名，value为字段值 */
            List<Entity> entityList = SqlExecutor.query(conn, sql, new EntityListHandler(), args);
            logger.info("{}", entityList);
            return entityList;
        } catch (SQLException e) {
            logger.error("SQL error!", e);
        } finally {
            DbUtil.close(conn);
        }
        return Collections.emptyList();
    }
}
